package com.zigong.app.ui.adapter;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.LittleLifeManager;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.zigong.app.R;
import com.zigong.app.base.BaseActivity;
import com.zigong.app.base.BaseFragment;
import com.zigong.app.ui.adapter.holder.AnchorColumnHolder;
import com.zigong.app.ui.adapter.holder.AnchorFilesHolder;
import com.zigong.app.ui.adapter.holder.AnchorHomeDynamicsHolder;
import com.zigong.app.ui.adapter.holder.AnchorIntroHolder;
import com.zigong.app.ui.adapter.holder.AnchorRecommenHolder;
import com.zigong.app.ui.adapter.holder.AnchorRecommendHolder;
import com.zigong.app.ui.adapter.holder.AnchorRelateNewsHolder;
import com.zigong.app.ui.adapter.holder.AnchorTitleHolder;
import com.zigong.app.ui.adapter.holder.AnchorTitleTiHolder;
import com.zigong.app.ui.adapter.holder.AuthorFileHolder;
import com.zigong.app.ui.adapter.holder.BaseAnchorHolder;
import com.zigong.app.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/12 0012.
 */

public class AnchorHomePageAdapter extends BaseHolderAbsAdapter<IListShowData, BaseAnchorHolder<IListShowData>> {
    public static final int VIEWTYPE_RECOMMEND = 0;//主播推荐
    public static final int VIEWTYPE_INTRO = 1;//主播简介
    public static final int VIEWTYPE_TITLE = 2;//栏目的title
    public static final int VIEWTYPE_TITLES = 3;//带副标题的主播动态Title
    public static final int VIEWTYPE_COLUMN = 4;//主持栏目
    public static final int VIEWTYPE_DYNAMICS = 5;//主播动态
    public static final int VIEWTYPE_FILE = 6;//主播档案
    public static final int VIEWTYPE_HOME_FILE = 7;//主播档案
    public static final int VIEWTYPE_HOME_NEWS = 8;//主播档案

    // RecyclerView 的第一个item，肯定是展示StickyLayout的.
    public static final int FIRST_STICKY_VIEW = 1;
    // RecyclerView 除了第一个item以外，要展示StickyLayout的.
    public static final int HAS_STICKY_VIEW = 2;
    // RecyclerView 的不展示StickyLayout的item.
    public static final int NONE_STICKY_VIEW = 3;


    private LittleLifeManager littleLifeManager;
    private OnItemInternalClick playClick;

    private boolean isInAnchorDetail;

    private BaseActivity activity;
    private BaseFragment fragment;
    private ArrayList<Pair<String, String>> pairs;


    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setFragment(BaseFragment fragment) {
        this.fragment = fragment;
    }

    public void setPairs(ArrayList<Pair<String, String>> pairs) {
        this.pairs = pairs;
    }

    public OnItemInternalClick getPlayClick() {
        return playClick;
    }

    public void setPlayClick(OnItemInternalClick playClick) {
        this.playClick = playClick;
    }

    public AnchorHomePageAdapter(Context context, List list) {
        super(context, list);
        littleLifeManager = new LittleLifeManager();
    }

    @Override
    public BaseAnchorHolder<IListShowData> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseAnchorHolder bah = null;
        View view = null;
        switch (viewType) {
            case VIEWTYPE_RECOMMEND://主播推荐
//                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_recommen, parent, false);
//                bah = new AnchorRecommenHolder(context, view, data);
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_recommend, parent, false);
                bah = new AnchorRecommendHolder(context, view);
                break;
            case VIEWTYPE_INTRO://主播简介
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_intro, parent, false);
                bah = new AnchorIntroHolder(context, view);
                break;
            case VIEWTYPE_TITLE://栏目的title
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_title, parent, false);
                bah = new AnchorTitleHolder(context, view);
                break;
            case VIEWTYPE_TITLES://带副标题的主播动态Title
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_title, parent, false);
                bah = new AnchorTitleTiHolder(context, view);
                bah.setInternalClick(this);
                break;
            case VIEWTYPE_COLUMN://主持栏目
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_column, parent, false);
                bah = new AnchorColumnHolder(context, view);
                break;
            case VIEWTYPE_DYNAMICS://主播动态（主播发布的新闻等内容，点击头像可进入主播的个人主页）
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_home_dynamics, parent, false);
                bah = new AnchorHomeDynamicsHolder(context, view, data).setInAnchorDetail(isInAnchorDetail);
                bah.setInternalClick(playClick);
                break;
            case VIEWTYPE_FILE://主播档案
                view = LayoutInflater.from(context).inflate(R.layout.item_author_file, parent, false);
                bah = new AuthorFileHolder(context, view);
                bah.setInternalClick(playClick);
                break;
            case VIEWTYPE_HOME_FILE:
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_file, parent, false);
                bah = new AnchorFilesHolder(context, view);
                break;

            case VIEWTYPE_HOME_NEWS:
                view = LayoutInflater.from(context).inflate(R.layout.item_anchor_relate, parent, false);
                bah = new AnchorRelateNewsHolder(context, view);
                bah.setInternalClick(playClick);
                break;

        }
        bah.setPairs(pairs);
        bah.setActivity(activity);
        bah.setFragment(fragment);
        return bah;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(BaseAnchorHolder<IListShowData> holder, int position) {
//        holder.setItemMode(position == getItemCount() - 1 ? BaseAnchorHolder.ITEMMODE_LAST : BaseAnchorHolder.ITEMMODE_NORMAL);
        super.onBindViewHolder(holder, position);
    }

    @Override
    public void onViewAttachedToWindow(BaseAnchorHolder<IListShowData> holder) {
        super.onViewAttachedToWindow(holder);
//        if (holder instanceof IAndroidLittleLife) {
//            IAndroidLittleLife ia = (IAndroidLittleLife) holder;
//            ia.setLifeMode(1);
//            if (ia.hasInManager()) {
//                ia.onIResume();
//            } else {
//                addLife(ia);
//            }
//        }
    }

    @Override
    public void onViewDetachedFromWindow(BaseAnchorHolder<IListShowData> holder) {
        super.onViewDetachedFromWindow(holder);
//        if (holder instanceof IAndroidLittleLife) {
//            IAndroidLittleLife ia = (IAndroidLittleLife) holder;
//            if (ia.hasInManager()) {
//                ia.onIPause();
//            }
//            ia.setLifeMode(0);
//        }
    }

    @Override
    public void onResume() {
        littleLifeManager.onResume();
    }

    @Override
    public void onPause() {
        littleLifeManager.onPause();
    }

    @Override
    public void onDestory() {
        littleLifeManager.onDestory();
    }

    protected void addLife(IAndroidLittleLife ia) {
        littleLifeManager.addLifeInf(ia);
    }

    @Override
    public AnchorHomePageAdapter setData(List data) {
        super.setData(data);
        return this;
    }

    @Override
    public void addDatas(List list) {
        if (data == null) {
            if (ListUtils.isListValued(list)) {
                data = new ArrayList<>();
                data.addAll(list);
                notifyDataSetChanged();
            }
        } else if (list != null && list.size() > 0) {
            int fromPos = getItemCount();
            if (fromPos < 0) {
                fromPos = 0;
            }
            data.addAll(list);
            notifyItemRangeChanged(fromPos, list.size());
            notifyDataSetChanged();
        }
    }

    @Override
    public AnchorHomePageAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    public void setInAnchorDetail(boolean inAnchorDetail) {
        isInAnchorDetail = inAnchorDetail;
    }
}
